<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<!--{include file='comm/header.tpl'}-->
<link rel="stylesheet" type="text/css" href="/framework/JSCal2/src/css/jscal2.css" />
<link rel="stylesheet" type="text/css" href="/framework/JSCal2/src/css/border-radius.css" />
<link rel="stylesheet" type="text/css" href="/framework/JSCal2/src/css/steel/steel.css" />
<script src="/framework/JSCal2/src/js/jscal2.js"></script>
<script src="/framework/JSCal2/src/js/lang/cn.js"></script>
<style>
	li{
		width:164px;
		height:24px;
		line-height:24px;
		text-align:left;
		display:block;
		float:left;
	}
</style>
<script language="JavaScript" src="/quanliadmin/docroot/js/region.js" type="text/javascript"></script>
    <!--{if $operFlg == 1}-->
    <title>配送区域添加</title>
    <!--{else}-->
    <title>配送区域编辑</title>
    <!--{/if}-->
<script>
	$(document).ready(function(){
		var operFlg = '<!--{$operFlg}-->';
		
		provinceStr = '<option value="">请选择</option>';//省
			
		$.each(region_obj,function(i,item){
			provinceStr += '<option value="'+i+'">'+item.n+'</option>';
		});
		
		$('#province_select').html(provinceStr);
		if (operFlg == 2){
			var regionArr = '<!--{$regionArr}-->';//原区域数组
			var sendRegion = eval("("+regionArr+")");
			var checkArr = '<!--{$checkArr}-->';//原区域ID组成的数组
			var checkID = eval("("+checkArr+")");
			source.publicRegion = sendRegion;
			source.checkRegionID = checkID;
			source.addRegion();
			source.changeTag();
		}
	});
	var source = {
		region: region_obj,//区域的json数组
		
		publicRegion: new Array(),//区域参数数组
		
		checkRegionID: new Array(),//检测是否已选择的数组
		//省、市、区、县四级联动菜单
		changeRegion: function(){
			regionObj = this.region;//所有区域的JSON
			
			var cityStr = '<option value="">请选择</option>';
			var countryStr = '<option value="">请选择</option>';
			var townStr = '<option value="">请选择</option>';
			
			var provinceValue = $('#province_select').val();
			var cityValue = $('#city_select').val();
			var countryValue = $('#country_select').val();
			var townValue = $('#town_select').val();
			
			$.each(regionObj,function(i,itemProvince){
				
				//省级的值不为空则循环得到市级
				if (i == provinceValue && provinceValue != ''){
					$.each(itemProvince.c,function(j,itemCity){
						cityStr += '<option value="'+j+'">'+itemCity.n+'</option>';
						
						//市级值不为空则循环得到县级
						if (j == cityValue && cityValue != ''){
							$.each(itemCity.c,function(k,itemCountry){
								countryStr += '<option value="'+k+'">'+itemCountry.n+'</option>';
								
								//县级不为空则循环得到镇级
								if(k == countryValue && countryValue != ''){
									//镇级存在
									if(itemCountry.c){
										$.each(itemCountry.c,function(m,itemTown){
											townStr += '<option value="'+m+'">'+itemTown+'</option>';
										});
									}
									$('#town_select').html(townStr);
								}
							});
							$('#country_select').html(countryStr);
							$('#town_select').html(townStr);//选择区级则清空镇级
						}
					});
					$('#city_select').html(cityStr);
					$('#town_select').html(townStr);//清空镇级
					$('#country_select').html(countryStr);//清空区级
				}else if (provinceValue == '') {
					$('#city_select').html(cityStr);
					$('#town_select').html(townStr);//清空镇级
					$('#country_select').html(countryStr);//清空区级
				}				
			});
			
			if(cityValue != '')
				$('#city_select').val(cityValue);
			if(countryValue != '')
				$('#country_select').val(countryValue);
		},
		//将所选区域添加到页面上
		addRegion: function(){
			
			var provinceValue = $('#province_select').val();
			var provinceText = $('#province_select').find("option:selected").text();
			var cityValue = $('#city_select').val();
			var cityText = $('#city_select').find("option:selected").text();
			var countryValue = $('#country_select').val();
			var countryText = $('#country_select').find("option:selected").text();
			var townValue = $('#town_select').val();
			var townText = $('#town_select').find("option:selected").text();
			
			//检测当前选择的区域ID在checkRegionID中是否存在，不存在则将能取到的最小一级id及其对应的区域名称、区域等级保存到publicRegion中
			if (townValue && townValue != ''){
				
				if ($.inArray(townValue,this.checkRegionID) == -1){
					region = {'id':townValue+'_4', 'name':townText, 'level': '4'};
					this.checkRegionID.push(townValue);
					this.publicRegion.push(region);
				}else {
					E.alert('区域 ['+townText+'] 已存在');
				}
				
			}else if(countryValue && countryValue != ''){
				
				if ($.inArray(countryValue,this.checkRegionID) == -1){
					region = {'id':countryValue+'_3', 'name':countryText, 'level': '3'};
					this.checkRegionID.push(countryValue);
					this.publicRegion.push(region);
				}else {
					E.alert('区域 ['+countryText+'] 已存在');
				}
				
			}else if(cityValue && cityValue != ''){
				
				if ($.inArray(cityValue,this.checkRegionID) == -1){
					region = {'id':cityValue+'_2', 'name':cityText, 'level': '2'};
					this.checkRegionID.push(cityValue);
					this.publicRegion.push(region);
				}else {
					E.alert('区域 ['+cityText+'] 已存在');
				}
				
			}else if(provinceValue && provinceValue != ''){
				//检测该区域ID是否已存在
				if ($.inArray(provinceValue,this.checkRegionID) == -1){
					region = {'id':provinceValue+'_1', 'name':provinceText, 'level': '1'};
					this.checkRegionID.push(provinceValue);
					this.publicRegion.push(region);
				}else {
					E.alert('区域 ['+provinceText+'] 已存在');
				}
				
			}
			
			//从publicRegion中取出对应的区域展示到页面
			if (this.publicRegion.length != 0){
				
				var ul_province = '';
				var ul_city = '';
				var ul_country = '';
				var ul_town = '';
				
				$.each(this.publicRegion,function(i,item){
					if (item.level == '1'){
						ul_province += '<li><input type="checkbox" name="region[]" checked="checked" value="'+item.id+'">'+item.name+'</li>';
					}
					if (item.level == '2'){
						ul_city += '<li><input type="checkbox" name="region[]" checked="checked" value="'+item.id+'">'+item.name+'</li>';
					}
					if (item.level == '3'){
						ul_country += '<li><input type="checkbox" name="region[]" checked="checked" value="'+item.id+'">'+item.name+'</li>';
					}
					if (item.level == '4'){
						ul_town += '<li><input type="checkbox" name="region[]" checked="checked" value="'+item.id+'">'+item.name+'</li>';
					}
				});
				
				$('#province').html(ul_province);
				$('#city').html(ul_city);
				$('#country').html(ul_country);
				$('#town').html(ul_town);
			}
		},
		//添加全部区域
		addAllRegion: function(){
			regionObj = this.region;
			
			regionArr = new Array();
			
			checkIDArr = new Array();
			
			$.each(regionObj,function(i,item){
				region = {'id':i+'_1', 'name':item.n, 'level': '1'};
				regionArr.push(region);
				checkIDArr.push(i);
			});
			
			this.publicRegion = regionArr;
			this.checkRegionID = checkIDArr;
			
			var ul_province = '';
			
			$.each(this.publicRegion,function(i,item){
				if (item.level == '1'){
					ul_province += '<li><input type="checkbox" name="region[]" checked="checked" value="'+item.id+'">'+item.name+'</li>';
				}
			});
			
			$('#province').html(ul_province);
			$('#city').html('');
			$('#country').html('');
			$('#town').html('');
		},
		//保存区域
		edit: function(){
			
			var send_region_name = $('#send_region_name').val();//配送区域名
			var freight = $('#freight').val();//标准运费
			var standardFlg = $("input[name='standardFlg']:checked").val();//标准模式
			var standard = $('#standard').val();//标准运费对应的重量或体积
			var overStandard = $('#overStandard').val();//超标运费
//			var freeBillMoney = $('#freeBillMoney').val();//优惠运费订单下限
//			var promotionFreight = $('#promotionFreight').val();//优惠后运费
//			var startDate = $('#startDate').val();//优惠开始时间
//			var endDate = $('#endDate').val();//优惠结束时间
			var regionBox = $("input:checkbox[name='region[]']:checked");//区域
			var regionArr = new Array();//区域对应的数组
			var sendTypeID = $('#sendTypeID').val();
			
			var error_msg = '';
			if (sendTypeID == ''){
				return E.alert('请先保存配送方式基本信息后再编辑配送区域');
			}
			if (send_region_name == ''){
				error_msg += '配送区域名称不能为空<br/>';
			}
			if (freight == '' || !E.isMoney(freight)){
				error_msg += '标准运费不能为空且应符合货币格式<br/>';
			}
			if (!E.empty(standardFlg) && standard == ''){
				error_msg += '标准模式所对应的重量或体积不能为空<br/>';
			}
//			if (freeBillMoney != '' && promotionFreight == ''){
//				error_msg += '优惠后的运费不能为空<br/>';
//			}
//			if (freeBillMoney == '' && promotionFreight != ''){
//				error_msg += '运费订单下限不能为空<br/>';
//			}
//			if ((freeBillMoney != '' || promotionFreight != '') && (startDate == '' || endDate == '')){
//				error_msg += '优惠时间不能为空<br/>';
//			}
//			if ((startDate != '' && endDate == '') || (startDate == '' && endDate != '')){
//				error_msg += '请填写完整的优惠时间<br/>';
//			}
//			if ((startDate != '' || endDate != '') && (freeBillMoney == '' || promotionFreight == '')){
//				error_msg += '请填写完整的优惠信息<br/>';
//			}
//			if (startDate != '' && endDate != '' && endDate < startDate){
//				error_msg += '开始时间不能大于结束时间<br/>';
//			}
			if (regionBox.length == 0){
				error_msg += '区域信息不能为空<br/>';
			}else{
				for (var i=0;i<regionBox.length;i++){
					regionArr.push(regionBox.eq(i).val());
				}
			}
			if (error_msg != ''){
				return E.alert(error_msg);
			}
			var addForm = E.getFormValues('addForm');
			$.ajax({
				type: "POST",
				<!--{if $operFlg == 1}-->
                url: "/invokingAjax-backend/system/sendType.ajax?operFlg=6",
				<!--{else}-->
				url: "/invokingAjax-backend/system/sendType.ajax?operFlg=7",
				<!--{/if}-->
				dataType: "JSON",
				data:{'param': addForm, 'regionArr': regionArr},
				success: function( result_obj ) {
                    if (result_obj.code == 200) {
                        E.opener("source.load");
						<!--{if $operFlg == 1}-->
						E.alert(result_obj.message,  2, "source.clear");
						<!--{else}-->
						E.alert(result_obj.message,  2, "E.closeWindows");
						<!--{/if}-->
                    } else {
                        E.alert(result_obj.message);
                    }
                }
			});
		},
		//切换标准模式
		changeTag: function(){
			var standard = $('input:radio[name="standardFlg"]:checked').val();
			if (standard == 1){
				$('#tag_1').html('公斤');
				$('#tag_2').html('元/公斤');
			}else if (standard == 2){
				$('#tag_1').html('立方米');
				$('#tag_2').html('元/立方米');
			}
		},
		clear:function(){
			document.forms['addForm'].reset();
			$('#province').html('');
			$('#city').html('');
			$('#country').html('');
			$('#town').html('');
			provinceStr = '<option value="">请选择</option>';//省
			cityStr = '<option value="">请选择</option>';
			countryStr = '<option value="">请选择</option>';
			townStr = '<option value="">请选择</option>';
			$.each(region_obj,function(i,item){
				provinceStr += '<option value="'+i+'">'+item.n+'</option>';
			});
			$('#province_select').html(provinceStr);
			$('#city_select').html(cityStr);
			$('#country_select').html(countryStr);
			$('#town_select').html(townStr);
		}
	};
</script>
</head>
<body>
	<div style="width:100%">
		<div class="items clearfix">
			<h2 style="" class="tal">
				<!--{if $operFlg == 1}-->
			    	配送区域添加
			    <!--{else}-->
			    	配送区域编辑
			    <!--{/if}-->
			</h2>
			
			<div class="items2">
				<!--{if $error_msg}-->
	            
				<div class="comm-box">
	                <div style="position:relative;color: red;">
	                    <!--{$error_msg}-->
	                </div>
	            </div>
				
	            <!--{else}-->
				
				<form id="addForm" name="addForm" onsubmit="return false;">
					<table>
						<tr>
							<th colspan="2" style="text-align:left;">配送区域基本信息</th>
						</tr>
						<tr>
							<td width="150" class="tar">配送区域名称：</td>
							<td width="*" class="td_input">
								<input type="text" style="width:200px" id="send_region_name" name="send_region_name" value="<!--{$regionInfo->send_region_name}-->">
								<input type="hidden" id="send_region_id" name="send_region_id" value="<!--{$regionInfo->send_region_id}-->">
								<input type="hidden" id="sendTypeID" name="sendTypeID" value="<!--{$sendTypeID}-->">
							</td>
						</tr>
						<tr>
							<td class="tar">标准运费：</td>
							<td class="td_input">
								<input type="text" style="width:200px" id="freight" name="freight" value="<!--{$regionInfo->freight}-->">
							</td>
						</tr>
						<tr>
							<td class="tar">标准模式：</td>
							<td class="td_input">
								<input type="radio" id="standard_1" name="standardFlg" value="1" <!--{if $regionInfo->standardFlg == 1}-->checked="checked"<!--{/if}--> onchange="source.changeTag()">&nbsp;<label for="standard_1">重量</label>&nbsp;&nbsp;
								<input type="radio" id="standard_2" name="standardFlg" value="2" <!--{if $regionInfo->standardFlg == 2}-->checked="checked"<!--{/if}--> onchange="source.changeTag()">&nbsp;<label for="standard_2">体积</label>&nbsp;
								<input type="text" style="width:70px" id="standard" name="standard" value="<!--{$regionInfo->standard}-->">&nbsp;<span id="tag_1"></span>
							</td>
						</tr>
						<tr>
							<td class="tar">超标运费：</td>
							<td class="td_input">
								<input type="text" style="width:200px" id="overStandard" name="overStandard" value="<!--{$regionInfo->overStandard}-->">&nbsp;<span id="tag_2"></span>
							</td>
						</tr>
						<!--<tr>
							<th colspan="2" style="text-align:left;">优惠信息</th>
						</tr>
						<tr>
							<td class="tar">运费订单下限：</td>
							<td class="td_input">
								<input type="text" style="width:200px" id="freeBillMoney" name="freeBillMoney" value="<!--{$regionInfo->freeBillMoney}-->">
							</td>
						</tr>
						<tr>
							<td class="tar">优惠后运费：</td>
							<td class="td_input">
								<input type="text" style="width:200px" id="promotionFreight" name="promotionFreight" value="<!--{$regionInfo->promotionFreight}-->">
							</td>
						</tr>
						<tr>
							<td class="tar">优惠时间：</td>
							<td class="td_input">
								<input type="text" id="startDate" name="startDate" style="width:125px;" readonly="readonly" value="<!--{$regionInfo->startDate}-->">
                                ～
                                <input type="text" id="endDate" name="endDate" style="width:125px;" readonly="readonly" value="<!--{$regionInfo->endDate}-->">&nbsp;
							</td>
						</tr>-->
						<tr>
							<th colspan="2" style="text-align:left;">区域信息</th>
						</tr>
						<tr>
							<td class="td_input" colspan="2">
								<div style="height:160px;float:left;">省：</div>
								<div style="height:160px;float:left;margin-right:20px;">
									<select id="province_select" name="province_select" style="width:100px;height:150px;" size="20" onchange="source.changeRegion();"></select>
								</div>
								<div style="height:160px;float:left;">市：</div>
								<div style="height:160px;float:left;margin-right:20px;">
									<select id="city_select" name="" style="width:100px;height:150px;" size="20" onchange="source.changeRegion();">
							   			<option value="">请选择</option>
							  	   	</select>
								</div>
								<div style="height:160px;float:left;">区：</div>
								<div style="height:160px;float:left;margin-right:20px;">
									<select id="country_select" name="country_select" style="width:100px;height:150px;" size="20" onchange="source.changeRegion();">
										<option value="">请选择</option>
								   	</select>
								</div>
								<div style="height:160px;float:left;">镇：</div>
								<div style="height:160px;float:left;margin-right:20px;">
									<select id="town_select" name="town_select" style="width:100px;height:150px;" size="20">
										<option value="">请选择</option>
								  	</select>
								</div>
								<div style="height:160px;float:left;">
									<div style="margin-bottom:20px;">
										<input type="button" class="btn" value="  添  加  " onclick="source.addRegion();">
									</div>
									<div>
										<input type="button" class="btn" value="添加全部" onclick="source.addAllRegion();">
									</div>
								</div>
								<div style="clear:both;"></div>
							</td>
						</tr>
						<tr>
							<td class="tar">所选区域（省）：</td>
							<td class="td_input">
								<ul id="province"></ul>
								<div style="clear:both;"></div>
							</td>
						</tr>
						<tr>
							<td class="tar">所选区域（市）：</td>
							<td class="td_input">
								<ul id="city"></ul>
								<div style="clear:both;"></div>
							</td>
						</tr>
						<tr>
							<td class="tar">所选区域（区）：</td>
							<td class="td_input">
								<ul id="country"></ul>
								<div style="clear:both;"></div>
							</td>
						</tr>
						<tr>
							<td class="tar">所选区域（镇）：</td>
							<td class="td_input">
								<ul id="town"></ul>
								<div style="clear:both;"></div>
							</td>
						</tr>
					</table>
				</form>
				
				<!--{/if}-->
			</div>
		</div>
		
		<div class="bottom_fixbox">
			<div class="rectbox tac">
	        	<!--{if $operFlg == 1}-->
	        		<input type="button" class="btn" onclick="source.edit();" value="添加配送区域">
				<!--{else if $operFlg == 2}-->
					<input type="button" class="btn" onclick="source.edit();" value="修改配送区域">
				<!--{/if}-->
				<input type="button" class="btn" onclick="E.closeWindows();" value="关闭">
	        </div>
		</div>

	</div>
</body>
</html>
<script>
	//日期控件JS
    var cal = Calendar.setup({
        onSelect: function(cal) { cal.hide() }
    });
	cal.manageFields("startDate", "startDate", "%Y-%m-%d");
	cal.manageFields("endDate", "endDate", "%Y-%m-%d");
</script>